package cn.cubix.flow.service.impl;

import cn.cubix.flow.mapper.WfPrmsMapper;
import cn.cubix.flow.mapper.WfPrmsRoleMapper;
import cn.cubix.flow.mapper.WfRoleMapper;
import cn.cubix.flow.mapper.WfRoleUserMapper;
import cn.cubix.flow.service.WfRoleService;
import cn.cubix.flow.service.WfUserService;
import cn.cubix.flow.entity.*;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor(onConstructor=@__({@Autowired,@Lazy}))
public class WfRoleServiceImpl extends ServiceImpl<WfRoleMapper, WfRole> implements WfRoleService {

    private final WfRoleUserMapper wfRoleUserMapper;
    private final WfUserService wfUserService;
    private final WfPrmsRoleMapper wfPrmsRoleMapper;
    private final WfPrmsMapper wfPrmsMapper;

    @Override
    public void allocUser(String roleid, List<String> userids) {
        Assert.notEmpty(roleid);
        List<WfRoleUser> toSaveList = new ArrayList<>();
        List<WfRoleUser> existsList = wfRoleUserMapper
                .selectList(new LambdaQueryWrapper<WfRoleUser>().eq(WfRoleUser::getRoleId,roleid));
        Map<String,String> existsMap = existsList.stream().collect(
                Collectors.toMap(WfRoleUser::getUserId, WfRoleUser::getId));
        for(String userid : userids){
            // 接口传来的userid不存在于existsMap的key中，表示没有这个角色用户关系，那就新建
            // 如果有这个角色用户关系，被remove掉，就是被忽略，不删除
            if(existsMap.remove(userid)==null){
                WfRoleUser wfRoleUser = new WfRoleUser();
                wfRoleUser.setId(UUID.randomUUID().toString());
                wfRoleUser.setRoleId(roleid);
                wfRoleUser.setUserId(userid);
                toSaveList.add(wfRoleUser);
            }
        }
        // 剩下的userid关系，是数据库中有而接口没传的，删除掉
        if(!existsMap.isEmpty()){
            wfRoleUserMapper.deleteBatchIds(existsMap.values());
        }
        for(WfRoleUser toSave:toSaveList){
            wfRoleUserMapper.insert(toSave);
        }
    }

    @Override
    public void allocPrms(String roleid, List<String> prmsids) {
        Assert.notEmpty(roleid);
        List<WfPrmsRole> toSaveList = new ArrayList<>();
        List<WfPrmsRole> existsList = wfPrmsRoleMapper
                .selectList(new LambdaQueryWrapper<WfPrmsRole>().eq(WfPrmsRole::getRoleId,roleid));
        Map<String,String> existsMap = existsList.stream().collect(
                Collectors.toMap(WfPrmsRole::getPrmsId, WfPrmsRole::getId));
        for(String prmsid : prmsids){
            // 接口传来的prmsid不存在于existsMap的key中，表示没有这个角色用户关系，那就新建
            // 如果有这个角色用户关系，被remove掉，就是被忽略，不删除
            if(existsMap.remove(prmsid)==null){
                WfPrmsRole wfPrmsRole = new WfPrmsRole();
                wfPrmsRole.setId(UUID.randomUUID().toString());
                wfPrmsRole.setRoleId(roleid);
                wfPrmsRole.setPrmsId(prmsid);
                toSaveList.add(wfPrmsRole);
            }
        }
        // 剩下的prmsid关系，是数据库中有而接口没传的，删除掉
        if(!existsMap.isEmpty()){
            wfPrmsRoleMapper.deleteBatchIds(existsMap.values());
        }
        if(!toSaveList.isEmpty()){
            Map<String, WfPrms> wfPrmsMap = wfPrmsMapper.selectList(new LambdaQueryWrapper<WfPrms>()
                    .in(WfPrms::getId,toSaveList.stream().map(WfPrmsRole::getPrmsId).collect(Collectors.toList())))
                    .stream().collect(Collectors.toMap(WfPrms::getId,wfPrms->wfPrms));
            toSaveList.forEach(wfPrmsRole -> {
                WfPrms wfPrms = wfPrmsMap.get(wfPrmsRole.getPrmsId());
                if(wfPrms!=null){
                    wfPrmsRole.setDisabled(false);// TODO 默认启用 后面再改
                }
            });
        }
        for(WfPrmsRole toSave:toSaveList){
            wfPrmsRoleMapper.insert(toSave);
        }
    }

    @Override
    public List<WfUser> alcedUser(String roleid) {
        List<WfRoleUser> wfRoleUserList = wfRoleUserMapper.selectList(new LambdaQueryWrapper<WfRoleUser>().eq(WfRoleUser::getRoleId,roleid));
        List<String> wfUserIdList = wfRoleUserList.stream().map(WfRoleUser::getUserId).collect(Collectors.toList());
        return wfUserIdList.isEmpty()? Collections.emptyList():wfUserService.list(new LambdaQueryWrapper<WfUser>().in(WfUser::getId,wfUserIdList));
    }

    @Override
    public List<WfPrms> alcedPrms(String roleid) {
        List<WfPrmsRole> wfPrmsRoleList = wfPrmsRoleMapper.selectList(new LambdaQueryWrapper<WfPrmsRole>().eq(WfPrmsRole::getRoleId,roleid));
        List<String> wfPrmsIdList = wfPrmsRoleList.stream().map(WfPrmsRole::getPrmsId).collect(Collectors.toList());
        return wfPrmsIdList.isEmpty()? Collections.emptyList():wfPrmsMapper.selectList(new LambdaQueryWrapper<WfPrms>().in(WfPrms::getId,wfPrmsIdList));
    }
}
